﻿using System.Threading;
using Needle.Bus.Messages;

namespace Needle.Bus.Engine.Filters
{
    public class StageFilterBase : EngineComponent, IStageFilter
    {
  
        #region constructors

        public StageFilterBase(string id)
            : base(id)
        { }

        public StageFilterBase(object mutex, string id)
            : base(mutex, id)
        { }

        #endregion

        private int _isEnabled;
        public bool IsEnabled
        {
            get
            {
                return _isEnabled == 1;
            }
            set
            {
                int enable = value ? 1 : 0;
                Interlocked.CompareExchange(ref _isEnabled, enable, _isEnabled);
            }
        }

        public virtual void Update()
        {
            
        }

        public bool Filter(IMessage message)
        {
            if (!IsEnabled)
                return true;
            else
                return DoFilter(message);
        }

        protected virtual bool DoFilter(IMessage message)
        {
            return true;
        }

        protected override void OnOpen(System.TimeSpan timeout)
        {
            // TODO: implement
        }

        protected override void OnAbort()
        {
            // TODO: implement
        }

        protected override void OnClose(System.TimeSpan timeout)
        {
            // TODO: implement
        }
    }
}
